<?php
namespace jy_mfee;
defined('BY_JYA') or exit('error');
/*
	分销插件会员类
	如果将分销商各项数据在每次数据变动时计算好并存入数据库。
	那么分别需要做
		在设置上下级关系时，修改新上级的 下线数量、分销商数量、订单数量、订单佣金数量等数据
		在变更上下级关系时，修改原、新上级的各项参数
		在发生订单时，修改上级的订单相关参数
		在用户成为分销商时，修改上级的分销商数量相关数据
*/
class member extends \plugin{
	/*
		核实分销商,$isset无条件成为分销商
	*/
	public function checkmember($mid=0,$isexi=false,$isset=false){
			$mid=$mid?:$_SESSION['uid'];
			$member=$this->get('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));	
			$page_setting=$this->plugin_setting('page_setting');
			$page_setting['tagent']=$page_setting['tagent']?:'代理商';
			if(!$member['mid']){
				$this->register(array('mid'=>$mid));
				$member=$this->get('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid));
			}
			if(!$member['is_agent']){
				//如果成为分销商无条件，此处自动申请成为分销商
				if($this->setting['become']==0 || $isset || $this->session('becomefromuser')){
					//分销商无需审核，此处自动审核
					$ispass=true;
					$this->apply_agent($member['mid']);
					if($this->setting['become_check'] && $isexi){
						exi('申请正在审核请耐心等待！','error','jy_mfee/apply/');
					}elseif($isset){
						return true;
					}
				}elseif($this->setting['become']==2){
					//消费次数
					$buynums=pdo_count('jy_weishop_order',array('merchid'=>$_SESSION['onemerchid'],'ispay'=>1,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'id');
					if($buynums>=$this->setting['num']){
						$ispass=true;
						$this->apply_agent($member['mid']);
						if($this->setting['become_check'] && $isexi){
							exi('申请正在审核请耐心等待。','error','jy_mfee/apply/');
						}
					}elseif($isexi){
						exi('你还不是'.$page_setting['tagent'].'，请前往申请！','error','jy_mfee/apply/');
					}
				}elseif($this->setting['become']==3){
					//消费金额
					$buyfee=pdo_getsum('jy_weishop_order',array('merchid'=>$_SESSION['onemerchid'],'ispay'=>1,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'price');
					if($buyfee>=$this->setting['money']){
						$ispass=true;
						$this->apply_agent($member['mid']);
						if($this->setting['become_check'] && $isexi){
							exi('申请正在审核请耐心等待。','error','jy_mfee/apply/');
						}
					}elseif($isexi){
						exi('你还不是'.$page_setting['tagent'].'，请前往申请！','error','jy_mfee/apply/');
					}
				}elseif($this->setting['become']==4){
					//购买指定商品
					$become_goods=$this->setting['become_goods'];
					foreach($become_goods as &$bgv){
						$isbuy=pdo_count('jy_weishop_order_goods',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$member['mid'],'goodsid'=>$bgv['id'],'ispay'=>1),'id');
						if($isbuy>0){
							$ispass=true;
							$this->apply_agent($member['mid']);
							break;
						}
					}
					if($this->setting['become_check'] && $isexi && $ispass){
						exi('您的申请，正在审核请耐心等待！','error','jy_mfee/apply/');
					}elseif($isexi && !$ispass){
						exi('你还不是'.$page_setting['tagent'].'，请前往申请！','error','jy_mfee/apply/');
					}
				}else{
					if($isexi){
						exi('你还不是'.$page_setting['tagent'].'，请前往申请！','error','jy_mfee/apply/');
					}
				}
				if($ispass){
					exi('','','jy_mfee/index/');
				}
			}elseif($member['stop'] && $isexi){
				if($isset){
						return true;
				}
				exi('你的'.$page_setting['tagent'].'身份已禁用！','error','jy_mfee/apply/');
			}elseif((!$member['realname'] || !$member['mobile']) && $isexi){
				if($isset){
					return true;
				}
				if($this->setting['become_reg']==1){
					exi('请先完善信息','error','jy_mfee/info/');
				}else{
					
				}
			}
			return $member;
	}
	public function checklogin(){
		if(!$_SESSION['uid']){
			exi('','','jy_mfee/login/');
		}
		return true;
	}
	public function member_update($mid,$update=array()){
		pdo_update('jy_mfee_member',$update,array('mid'=>$mid));
		return true;
	}
	public function check_myshop($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$member=model('core/member')->basic($mid);
		$getmember=$this->get('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		if($getmember['is_agent']){
			$myshop=$this->get('shop',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
			if(!$myshop['id']){
				$data=array(
					'logo'=>$member['avatar'],
					'title'=>$member['title']?:$member['nickname'],
					'mid'=>$member['mid'],
					'uniacid'=>$_SESSION['uniacid'],
					'merchid'=>$_SESSION['onemerchid'],
				);
				pdo_insert('jy_mfee_shop',$data);
				return true;
			}
		}
		return false;
	}
	/*
		会员注册
	*/
	public function register($member=array()){
		if(!$member['uniacid']){
			$member['uniacid']=$_SESSION['uniacid'];
		}
		if(!$member['mid']){
			$member['mid']=$_SESSION['uid'];
		}		
		if(!$member['createtime']){
			$member['createtime']=TIMESTAMP;
		}
		if(!$member['mid']){
			return error('分销商会员注册失败，缺少会员ID.');
		}
		$ismid=$this->get('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$member['mid']),array('mid'));
		if($ismid){
			return false;
		}
		//成交保护期刷新
		$member['order_time']=TIMESTAMP;
		$member['merchid']=$_SESSION['onemerchid'];
		$this->insert('member',$member);
		if($member['agent_id']){
			$this->set_parent($member['mid'],$member['agent_id']);
		}
		if(!$member['agent_id'] && $_SESSION['this']=='custom' && $this->setting['become_child']==1){
			$agent_id=pdo_getcolumn('core_members',array('id'=>$_SESSION['uid']),'fromuser');
			if($agent_id){
				$this->set_parent($member['mid'],$agent_id);
			}
		}
		//如果成为分销商无条件，此处自动申请成为分销商
		if($this->setting['become']==0 || $this->session('becomefromuser')){
			//分销商无需审核，此处自动审核
			$this->apply_agent($member['mid'],true);
		}
	}
	/*
		设置上下级关系
	*/
	public function set_parent($mid,$parent_id,$ischange=false){
		$member=$this->get('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid),array('mid','is_agent','team_mid','level_id','agent_id','createtime'));
		//启用《排位分销》
		if(!$member['team_mid'] && $this->plugin_exists('jy_paiweifee')){
			$jy_paiweifee=plugin('jy_paiweifee')->plugin_setting('basic');
			if($jy_paiweifee['open']){
				$parent_id=model('jy_paiweifee/model')->bumember($member,$parent_id);
			}
		}
		$old_agent_id=$member['agent_id'];
		$isuser=pdo_getcolumn('core_members',array('id'=>$parent_id),'id');
		//如果不存在该会员，已经是分销商或已经有上级了，取消执行。
		if(($old_agent_id || !$isuser || ($member['is_agent'] && $member['createtime']+60<TIMESTAMP)) && !$ischange){
			return true;
		}
		//如果他的上级是我
		$ta_agent_id=$this->getcolumn('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$parent_id),'agent_id');
		if($ta_agent_id==$mid){
			if($ischange){
				exi('上级的上级不能是你自己！','error');
			}
			return true;
		}
		//如果上级是自己，上级ID归零
		if($mid==$parent_id){
			if($ischange){
				exi('上级不能是你自己！','error');
			}
			return true;
		}
		//启用《代理补位分销》
		if($this->plugin_exists('jy_bufee')){
			$jy_bufeeset=plugin('jy_bufee')->plugin_setting('basic');
			if($jy_bufeeset['open']){
				$parent_id=model('jy_bufee/model')->bumember($member,$parent_id);
			}
		}
		$this->update('member',array('agent_id'=>$parent_id),array('mid'=>$mid));
		//上级获得抽奖机会
		$gamepost=array(
			'type'=>8,
			'level_id'=>$member['level_id'],
		);
		model('jy_weishop/game')->up_addgame($gamepost,$parent_id);
		if($this->setting['level']>0){
			$agent_id=$old_agent_id;
			for($i=0;$i<$this->setting['level'];$i++){
				if(!$agent_id){
					break;
				}
				$agent_id=$this->getcolumn('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$agent_id),'agent_id');
			}
			$agent_id=$parent_id;
			for($i=0;$i<$this->setting['level'];$i++){
				if(!$agent_id){
					break;
				}
				//只有一级才增加一级下级
				if($i==0){
					$member_update=array(
						'member_num +='=>1,
						'agent_num +='=>1,
						'member_num_next +='=>1,
						'agent_num_next +='=>1,
					);
				}else{
					$member_update=array(
						'member_num +='=>1,
						'agent_num +='=>1,
					);
				}
				$this->update('member',$member_update,array('merchid'=>$_SESSION['onemerchid'],'mid'=>$agent_id));
				$this->update_agent_level($agent_id);
				model('jy_weishop/notice')->notice_fenxiao_xiajiadd($mid,$agent_id);
				$agent_id=$this->getcolumn('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$agent_id),'agent_id');
			}
		}
		return true;
	}
	/*
		申请成为分销商
	*/
	public function apply_agent($mid,$noapply=false){
		$member=$this->pdo_get('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		if($member['sale_mid']){
			exi('已经是直销用户，不能成为分销商。','error');
		}
		$apply=$this->get('apply',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		if($apply && $apply['status']!=2){
			if($apply['status']==1){
				$member_update=array(
					'is_agent'=>1,
					'agenttime'=>TIMESTAMP,
				);
				$this->update('member',$member_update,array('merchid'=>$_SESSION['onemerchid'],'mid'=>$apply['mid']));
				$this->upyao_fee($mid);
				$this->model('notice')->notice_fenxiao_add($mid);
			}
			return;
		}
		if($apply['status']!=2){
			$new_apply=array(
				'uniacid'=>$_SESSION['uniacid'],
				'merchid'=>$_SESSION['onemerchid'],
				'mid'=>$mid,
				'status'=>0,
				'createtime'=>TIMESTAMP,
			);
			$this->insert('apply',$new_apply);
			$apply_id=$this->insertid();
			//开小店：
			$myshop=pdo_get('jy_mfee_shop',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
			if(!$myshop['mid']){
				$member=pdo_get('core_members',array('id'=>$mid),array('id','avatar','nickname'));
				$shop=array(
					'logo'=>$member['avatar'],
					'title'=>$member['nickname'].'的小店',
					'mid'=>$mid,
					'uniacid'=>$_SESSION['uniacid'],
					'merchid'=>$_SESSION['onemerchid'],
				);
				pdo_insert('jy_mfee_shop',$shop);
			}
		}else{
			$this->update('apply',array('status'=>0),array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
			$apply_id=$apply['id'];
		}
		if(!$this->setting['become_check']){
			//分销商无需审核，此处自动审核
			$this->check_apply($apply_id,1);
		}
		return true;
	}
	/*
		审核分销商
	*/
	public function check_apply($apply_id,$status,$remark=''){
		if(!$apply_id || !$status){
			return;
		}
		$apply=$this->get('apply',array('id'=>$apply_id));
		if(!$apply){
			return;
		}
		$update=array(
			'status'=>$status,
			'complete_time'=>TIMESTAMP,
		);
		if($update['status']==1){
			//审核通过，修改会员状态，修改上线相关数据
			$member_update=array(
				'is_agent'=>1,
				'agenttime'=>TIMESTAMP,
			);
			$this->update('member',$member_update,array('merchid'=>$apply['merchid'],'mid'=>$apply['mid']));
			//审核通过，此处增加上线的相关数据
			$agent_id=$this->getcolumn('member',array('merchid'=>$apply['merchid'],'mid'=>$apply['mid']),'agent_id');
			for($i=0;$i<$this->setting['level'];$i++){
				if(!$agent_id){
					break;
				}
				$agent_update=array('agent_num +='=>1);
				if($i==0){
					$agent_update['agent_num_next +=']=1;
				}
				$this->update('member',$agent_update,array('merchid'=>$apply['merchid'],'mid'=>$agent_id));
				$this->update_agent_level($agent_id);
				$agent_id=$this->getcolumn('member',array('merchid'=>$apply['merchid'],'mid'=>$agent_id),'agent_id');
			}
		}else{
			$update['remark']=$remark;
		}
		$this->update('apply',$update,array('id'=>$apply_id));
		return true;
	}
	/*
		禁用分销商
	*/
	public function stop_agent($agent_id){
		$this->update('member',array('stop'=>1),array('merchid'=>$_SESSION['onemerchid'],'mid'=>$agent_id));
	}
	/*
		恢复分销商
	*/
	public function start_agent($agent_id){
		$this->update('member',array('stop'=>0),array('merchid'=>$_SESSION['onemerchid'],'mid'=>$agent_id));
	}
	/*
		获取会员的各项参数
		
		问题：开启分销内购时
			推广订单如何算？
			下线如何算？
			
			计算下线时，分销级数在设置的级数基础上-1，其他不变
			计算推广订单时，在上面的基础上+自己的订单
	*/
	public function get_member($mid){
		$res=array();
		//获取一级下线数量
		$members=$this->getall('member',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'agent_id'=>$mid),array('mid','is_agent'),'mid');		
		if(!$members){
			$res=array(
				'member_num'=>'0',
				'agent_num'=>'0',
				'order_price'=>'0',
				'order_num'=>'0',
				'myorder_price'=>'0',
				'order_price_next'=>'0',
				'order_num_next'=>'0',
				'member_num_next'=>'0',
				'agent_num_next'=>'0',
			);
			return $res;
		}
		//获取直属下级分销商数量
		$res['agent_num_next']=$this->count('member',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'is_agent'=>1,'agent_id'=>$mid));
		$res['member_num_next']=count($members);
		$member_ids=array_keys($members);
		$res['order_num_next']=$this->count('order',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$member_ids));
		$res['order_price_next']=$this->sum('order',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$member_ids),'order_price');
		//获取下线总数，向下遍历系统设置的指定分销层数
		$commission_level=intval($this->setting['level']);
		if($this->setting['self_commission']){
			//开启分销内购
			$commission_level=$commission_level-1;
		}
		if($commission_level>1){
			$next_ids=$member_ids;
			for($i=1;$i<$commission_level;$i++){
				$next_members=$this->getall('member',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'agent_id'=>$next_ids),array('mid','is_agent'),'mid');
				if(!$next_members){
					break;
				}
				$members=array_merge($next_members,$members);
				$next_ids=array_keys($next_members);
				$member_ids=array_merge($next_ids,$member_ids);
			}
		}
		$res['member_num']=count($member_ids);
		$res['order_num']=$this->count('order',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$member_ids));
		$res['order_price']=$this->sum('order',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$member_ids),'order_price');
		$res['myorder_price']=$this->sum('order',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid),'myorder_price');
		$res['agent_num']=0;
		foreach($members as $member){
			if($member['is_agent']){
				$res['agent_num']++;
			}
		}
		if($this->setting['self_commission']){
			//开启分销内购，此处加上用户自己的订单作为推广订单的一部分
			$self_order_num=$this->count('order',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid));
			$self_order_money=$this->sum('order',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid),'order_price');
			$res['order_num_next']=$self_order_num;
			$res['order_price_next']=$self_order_money;
			$res['order_num']+=$self_order_num;
			$res['order_price']+=$self_order_money;
		}
		return $res;
	}
	/*
		升级分销商等级
		先检测会员身份是否为正常分销商
		然后获取分销商当前等级的下一等级升级条件
		然后逐个条件对比，看是否满足升级条件
		如果全部满足，则升级到相应等级
		
		何时触发升级分销商等级检测呢？
		
	*/
	public function update_agent_level($mid,$orderid=0){
		//
		$member=$this->get('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid));
		if(!$member['is_agent'] || $member['stop']){
			return;
		}
		$level=$this->get('level',array('id'=>$member['level_id']));
		$parameter=array(
			'params'=>array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'level >'=>$level['level']),
			'orderby'=>'level desc',
		);
		$levels=$this->getall('level',$parameter['params'],'','',$parameter['orderby']);
		if(empty($levels)){
			return;
		}
		$downmids=$this->getall('member',array('merchid'=>$_SESSION['onemerchid'],'agent_id'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']),'mid','mid');
		if($downmids){
			$downmids=array_keys($downmids);
		}
		$level_up_types=$this->model()->current_level_up_types();//获取当前设置的分销商等级升级条件
		$is_level_up_one=$this->setting['is_level_up_one'];
		foreach($levels as $lv){
			$conditions=json_decode($lv['conditions'],true);
			if(!$level_up_types || !$lv['id']){
				break;
			}
			$up=$is_level_up_one?false:true;
			foreach($level_up_types as $type){
				//留空默认满足
				if($is_level_up_one){
					if($type!='downbuy_goods' && $type!='buy_goods' && ($member[$type]>=$conditions[$type] || !$conditions[$type])){
						$up=true;
						break;
					}
				}else{
					if($type!='downbuy_goods' && $type!='buy_goods' && $member[$type]<$conditions[$type] && $conditions[$type]){
						$up=false;
						break;
					}
				}
				if(($type=='buy_goods' || $type=='downbuy_goods') && $lv['goods_ids']){
					$goods_ids=json_decode($lv['goods_ids'],true);
					foreach($goods_ids as $gv){
						    if($type=='buy_goods'){
								$isbuynum=pdo_getsum('jy_weishop_order_goods',array('merchid'=>$_SESSION['onemerchid'],'ispay'=>1,'mid'=>$mid,'goodsid'=>$gv['id']),'total');
								if($isbuynum>0){
									break;
								}
							}
							if($type=='downbuy_goods'){
								if($downmids && count($downmids)){
									$isbuynum=pdo_getnorepeat('jy_weishop_order_goods',array('merchid'=>$_SESSION['onemerchid'],'ispay'=>1,'mid' =>$downmids,'goodsid'=>$gv['id']),'mid');
									$isbuynum=count($isbuynum);
									if($isbuynum>=$conditions[$type]){
										break;
									}else{
										$isbuynum=0;
									}
								}
							}
					}
					if($is_level_up_one){
						if($isbuynum){
							$up=true;
							break;
						}
					}elseif(!$isbuynum){
						$up=false;
						break;
					}
				}elseif($type=='level_num'){
					//指定等级,指定等级人数（人）
					if($conditions['level_id']){
						$member['level_num']=$this->count('member',array('merchid'=>$_SESSION['onemerchid'],'level_id'=>$conditions['level_id'],'agent_id'=>$member['mid']));
					}
					if($is_level_up_one){
						if($member['level_num']>=$conditions['level_num'] || !$conditions['level_num']){
							$up=true;
							break;
						}
					}else{
						if($member['level_num']<$conditions['level_num'] && $conditions['level_num']){
							$up=false;
							break;
						}
					}
				}
			}
			if($up){
				$next_level=$lv;
				break;
			}
		}
		if($up){
			$update=array(
				'level_id'=>$next_level['id'],
			);
			if($next_level['feetype'] && $next_level['creditfee']){
				model('core/member')->member_credit($mid,'credit'.$next_level['feetype'],$next_level['creditfee'],true,'jy_mfee','达到指定分销等级获得奖励');
			}
			//上级获得抽奖机会
			$gamepost=array(
				'type'=>8,
				'level_id'=>$next_level['id']
			);
			model('jy_weishop/game')->up_addgame($gamepost,$member['agent_id']);
			$this->update('member',$update,array('mid'=>$mid));
			if($next_level['shoplevel_id']){
				model('jy_weishop/member')->up_member_level($mid,$next_level['shoplevel_id'],$_SESSION['onemerchid']);
			}
			$this->upyao_fee($mid);
		}
		if($isbuynum){
				if($this->setting['is_leveltime'] && $this->setting['leveltime']){
						$num=$isbuynum?:1;
						$member['t_time']+$num*$this->setting['leveltime']*24*3600;
						$nowtime=$up || $member['t_time']<TIMESTAMP?TIMESTAMP:$member['t_time'];
						$update=array(
								't_time'=>$nowtime+$num*$this->setting['leveltime']*24*3600
						);
						$this->update('member',$update,array('mid'=>$mid));
				}
		}
		return true;
	}
	public function min_agent_level($mid=0){
		//
		$mid=$mid?:$_SESSION['uid'];
		$member=$this->get('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid));
		//开启有效期后，降级
		if($this->setting['is_leveltime'] && $this->setting['leveltime']){
			if($member['t_time'] && $member['t_time']<TIMESTAMP){
				$level=$this->get('level',array('id'=>$member['level_id']));
				$this->update('member',array('level_id'=>$level['t_level_id'],'t_time'=>0),array('mid'=>$mid));
			}
		}
	}
	/*
		核实指定等级活动
	*/
	public function upgametime($mid_ids=array(),$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		if(count($mid_ids)){
			foreach($mid_ids as $mv){
				$level_id=$this->getcolumn('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mv),'level_id');
				$gamepost=array(
					'type'=>8,
					'level_id'=>$level_id
				);
				//model('jy_weishop/game')->up_addgame($gamepost,$mid);
			}
		}
	}
	/*
		邀请奖励
	*/
	public function upyao_fee($tomid=0){
		if(!$this->setting['yao_open']){
			return false;
		}
		$agent_id=$this->getcolumn('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$tomid),'agent_id');
		if(!$agent_id){
			return false;
		}
		if($this->setting['yao_open']){
			foreach($this->setting['yao_set'] as $k=>$yv){
				if($yv['yao_jitype'] && $tomid){
					$isget=$this->pdo_count('commission',array('merchid'=>$_SESSION['onemerchid'],'ordersn'=>'yao_set'.$tomid,'level'=>17,'mid'=>$agent_id,'uniacid'=>$_SESSION['uniacid']));
					if($isget>0){
						continue;
					}
					$money=$yv['fee'];
					if($yv['fee']>0 && $yv['yao_feetype']>0){
						model('core/member')->member_credit($agent_id,'credit'.$yv['yao_feetype'],$yv['fee'],true,'jy_mfee','邀请下级获得奖励');
						$jifen=$yv['fee'];
						$money=0;
					}
					$new_commission=array(
							'uniacid'=>$_SESSION['uniacid'],
							'mid'=>$agent_id,
							'level'=>17,
							'ordersn'=>'yao_set'.$tomid,
							'money'=>$money,
							'jifen'=>$jifen,
							'createtime'=>TIMESTAMP,
							'status'=>$yv['yao_feetype']>0?1:2,
					);
					$this->insert('commission',$new_commission);
				}elseif(!$yv['yao_jitype']){
					$num=$this->pdo_count('member',array('merchid'=>$_SESSION['onemerchid'],'level_id'=>$yv['level_id'],'agent_id'=>$agent_id,'uniacid'=>$_SESSION['uniacid']));
					if($yv['fee']>0 && $yv['num'] && $yv['num']<=$num){
						$isget=$this->pdo_count('commission',array('merchid'=>$_SESSION['onemerchid'],'ordersn'=>'yao_set'.$k,'level'=>17,'mid'=>$agent_id,'uniacid'=>$_SESSION['uniacid']));
						if($isget>0){
							continue;
						}
						$money=$yv['fee'];
						if($yv['fee']>0 && $yv['yao_feetype']>0){
							model('core/member')->member_credit($agent_id,'credit'.$yv['yao_feetype'],$yv['fee'],true,'jy_mfee','邀请下级获得奖励');
							$jifen=$yv['fee'];
							$money=0;
						}
						$new_commission=array(
							'uniacid'=>$_SESSION['uniacid'],
							'merchid'=>$_SESSION['onemerchid'],
							'mid'=>$agent_id,
							'level'=>17,
							'ordersn'=>'yao_set'.$k,
							'money'=>$yv['fee'],
							'createtime'=>TIMESTAMP,
							'status'=>$yv['yao_feetype']>0?1:2,
						);
						$this->insert('commission',$new_commission);
					}
				}
			}
		}
		return false;
	}
	/*
		签到奖励
	*/
	public function upsign_fee($mid=0,$credit_type=0,$fee=0){
		if(!$this->setting['sign_open'] || $this->setting['sign_rate']<=0 || $fee<=0 || !$credit_type){
			return false;
		}
		$agent_id=$this->getcolumn('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid),'agent_id');
		if(!$agent_id){
			return false;
		}
		$newfee=$this->setting['sign_rate']*$fee/100;
		if($newfee>0){
			model('core/member')->member_credit($agent_id,'credit'.$credit_type,$newfee,true,'jy_mfee','下级签到奖励');
			$new_commission=array(
				'uniacid'=>$_SESSION['uniacid'],
				'merchid'=>$_SESSION['onemerchid'],
				'mid'=>$agent_id,
				'level'=>23,
				'ordersn'=>'sign'.$mid,
				'jifen'=>$newfee,
				'createtime'=>TIMESTAMP,
				'status'=>2,
			);
			$this->insert('commission',$new_commission);
		}
		return true;
	}
	/*
		分销保护期
	*/
	public function upbao_time($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$this->update('member',array('order_time'=>TIMESTAMP),array('mid'=>$mid));
		return true;
	}
	/*
		刷新分销保护期关系
	*/
	public function upbao_agent($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		if(!$this->setting['bao_days']){
			return false;
		}
		$agent_ids=$this->getall('member',array('merchid'=>$_SESSION['onemerchid'],'agent_id'=>$mid),array('mid','order_time'));
		$bao_time=$this->setting['bao_days']*24*3600;
		foreach($agent_ids as $av){
			if($av['order_time'] && $av['order_time']+$bao_time<TIMESTAMP){
				$this->update('member',array('agent_id'=>0),array('mid'=>$av['mid']));
			}
		}
		return true;
	}
	/*
		申请升级
	*/
	public function leveapply_agent($post){
			$new_apply=array(
				'uniacid'=>$_SESSION['uniacid'],
				'merchid'=>$_SESSION['onemerchid'],
				'mid'=>$_SESSION['uid'],
				'realname'=>trim($post['realname']),
				'mobile'=>trim($post['mobile']),
				'wechat'=>trim($post['wechat']),
				'diyformdata'=>ijson_encode($post['diyformfields']),
				'status'=>0,
				'levelid'=>$this->setting['levefromid'],
				'createtime'=>TIMESTAMP,
			);
			$this->insert('leveapply',$new_apply);
			$apply_id=$this->insertid();
			return true;
	}
	/*
		审核升级
	*/
	public function check_leveapply($apply_id,$status,$remark=''){
		if(!$apply_id || !$status){
			return;
		}
		$apply=$this->get('leveapply',array('id'=>$apply_id));
		if(!$apply){
			return;
		}
		$update=array(
			'status'=>$status,
			'complete_time'=>TIMESTAMP,
		);
		if($update['status']==1){
			//审核通过，修改会员状态，修改上线相关数据
			$member_update=array(
				'level_id'=>$apply['levelid'],
			);
			$this->update('member',$member_update,array('mid'=>$apply['mid']));
		}else{
			$update['remark']=$remark;
		}
		$this->update('leveapply',$update,array('id'=>$apply_id));
		return true;
	}
}